<?php
  require_once '../../Helpers/DbHelper.php';
  require_once '../../Controllers/AdminController.php';
  require_once '../../Controllers/UserController.php';
  require_once '../../Helpers/PathHelper.php';
  //
  $u = new UserController();
  $u->evaluatePagePermission(UserController::ADMINISTRATOR);
  //
  $controller = new AdminController();
  $departmentList = $controller->getDepartmentList();
  $yearLevelList = $controller->getYearLevel();
  $subjectsList = $controller->getSubjectList();
  //
  if(isset($_POST) && !empty($_POST))
  {
     if('Add Year level' == $_POST['add'] && !empty($_POST['year-level-name']))
     {
       $yearLevelName = $_POST['year-level-name'];
       $departmentId = $_POST['department'];

       $yearStatus = $controller->addYearLevel($yearLevelName,$departmentId);
       $yearLevelList = $controller->getYearLevel();
       $subjectsList = $controller->getSubjectList();
     }
    else if('Add Subject' == $_POST['add'] && !empty($_POST['subject-name']))
    {
       $subjectName = $_POST['subject-name'];
       $yearLevel = $_POST['year'];

       $controller->addSubject($subjectName,$yearLevel);
       $subjectsList = $controller->getSubjectList();
    }
  }
  else
  {
   $subjectsList = $controller->getSubjectList();
  }
?>
<html>
  <head>
    <title>School Enrollment :: Manage Year Level and Subjects</title>
    <?php
      require_once '../commons.php';
      renderHeaderStyles() ;
    ?>
  </head>
  <body>
    <div class="admin-container">
      <?php renderAdminNav(); ?>
    </div>
    <div class="container">
        <div class="inner">
          <h3>ADD A YEAR LEVEL</h3>
          <form name="year-level-form" method="post">
            <table class="labels">
              <tr>
                <td><label for="add-year">Add a year level: </label></td>
                <td><input type="text" name="year-level-name" /></td>
              </tr>
              <tr>
                <td><label for="department-list">Choose a department:</label></td>
                <td><select name="department">
                <?php
                   while($rows = mysql_fetch_assoc($departmentList))
                   {
                     $i = $rows['DepartmentId'];
                     $n = $rows['DepartmentName'];
                     echo "<option value='$i'>$n</option>";
                   }
                ?>
                </select></td>
              </tr>
              <tr>
                <td>
                  <input type="submit" name="add" value="Add Year level" /><br /><br />
                  <?php
                    if($yearStatus)
                      echo 'Year Level added.'
                  ?>
              </tr>
            </table>
          </form><br /><br />
        </div>
        <hr />
        <h3>ADD A SUBJECT</h3>
        <form name="subjects-form" action="" method="post">
          <table class="labels">
            <tr>
              <td><label for="year-level">Choose a year level: </label></td>
              <td><select name="year">
                <?php
                  if(mysql_num_rows($yearLevelList) < 1)
                    echo "<option value='0'>No data available</option>";

                  while($rows=mysql_fetch_assoc($yearLevelList))
                  {
                    $i = $rows['YearLevelId'];
                    $n = $rows['YearLevelDescription'];
                    echo "<option value='$i'>$n</option>";
                  }
                ?>
              </select></td>
            </tr>
            <tr>
              <td><label for="subject-name">Subject Name:</label></td>
              <td><input type="text" name="subject-name" /></td>
            </tr>
            <tr>
              <td><input type="submit" name="add" value="Add Subject" /></td>
            </tr>
            </table>
        </form>
        <hr />
        <table cellpadding="0" width="600">
          <tr>
            <th>Year Level</th>
            <th>Subjects Available</th>
            <th>&nbsp;</th>
          </tr>
          <tr>
           <?php
            if(mysql_num_rows($subjectsList) < 1)
              echo '<td colspan="3">No subjects available.</td>';

            while($rows = mysql_fetch_assoc($subjectsList))
            {
              $yearLevel = $rows['YearLevelDescription'];
              $subject = $rows['Name'];
              $id = $rows['Id'];

              $content = '<tr>';
              $content.='<td>'.$yearLevel.'</td><td>'.$subject.'</td>';
              $content.='<td><a href="delete_subject.php?id='.$id.'">Delete</a></td>';
              $content.='</tr>';

              echo $content;
            }
           ?>
          </tr>
        </table>
    </div>
  </body>
</html>